*****************************************************************************
*	  Analysis Regional Exposure to Automation and Hate incidents Germany   *
*																			*			
* Author: 			Valentina Gonzalez Rostani		        				*
* Contact: 			mag384@pitt.edu              							*
* Date: 			August 21 2024   										*
* Version:			Stata 17                                                                                                       
*																			*
*****************************************************************************
/*
This do-file:
- Creates Table 4 and A16 using data from electoral perfomance of the AfD, hate incidents, and exposure to automation. 

Input:
- Data\Region_Germany\btw2017kreis (3).csv // This file contains electoral results. 
- Data\Region_Germany\ RegionEntries14.dta // This file contains replication data from  "Trade and Manufacturing Jobs in Germany" By Wolfgang Dauth, Sebastian Findeisen, and Jens Suedekum
- Data\Region_Germany\final_aggregated_data.dta// This file contains hate incidents in Germany, subset prepared from ARVIG data. The rmd which creates this file is named as  3_0_Regional_Germany_HateIncidents.rmd

- Alternatively you can go to line 79 and use prepared data: 
	- Data\Regional_Germany.dta

Output:
- Table 4: AfD Performance [Table\AfD_high_pop_r2.tex]
- Table A16: Summary statistics of variables used in this study about AfD regional performance [Table\AfD_descriptive.tex]

*/

*Defining Directory
cd "C:\Users\vgonz\Dropbox\Pitt\OneDrive for Business\Dissertation - Vale\Paper 2 - Political-Economic Polarization\Replication"

* Merging and preparing data - alternatively go to line 79
{
// Electoral data
{
import delimited "Data\Region_Germany\btw2017kreis (3).csv", varnames(5) clear 
drop if _n == 1
destring wahlberechtigte wähler ungültige gültige cdu spd dielinke grüne csu fdp afd npd freiewähler bp volksabstimmung pdv mlpd büso sgp dierechte tierschutzallianz b dkp diegrauen du mg menschlichewelt gesundheitsforschung vpartei dievioletten familie diefrauen mieterpartei neueliberale unabhängige übrige, replace

gen afp_prop = afd /gültige // valid vote

rename statistischekennziffer kreis
}
// Regional exposure of workers - Dauth et al 
{ 
merge 1:1 kreis using "Data\Region_Germany\RegionEntries14.dta"

drop if _merge<3 
drop _merge
}
// Hate incidents Arvig
{
merge 1:1 kreis using "Data\Region_Germany\final_aggregated_data.dta", force
 
replace anti=0 if anti==.
}
// Preparing clean file 
{
keep afd gültige afp_prop kreis anti routine  pop  perc_hq perc_foreign perc_female  perc_manuf_trad_nocars perc_manuf_auto  reg_south reg_east reg_north   state_n 

lab var perc_hq "Employment share of workers with University degree (\%)"
lab var perc_foreign "Employment share of Foreign Born (\%)"
lab var perc_female  "Employment share of Female (\%)"
lab var perc_manuf_trad_nocars "Employment share of other manuf. (\%)"
lab var  perc_manuf_auto "Employment share of manuf. of cars (\%)"
lab var afp_prop "AfD Share of votes"
lab var reg_south "South Region"
lab var reg_east "East Region"
lab var reg_north "North Region"
lab var anti "\# Hate incidents per district"
lab var routine "\# Routine workers"
lab var pop "Population"
lab var state_n "State number"

save "Data\Regional_Germany.dta", replace
}
}

* Alternatively you can call directly the data
use "Data\Regional_Germany.dta", clear

*******************************************************************************
* Preparing variables
*******************************************************************************
{
gen rou_pop=(routine/pop)
gen anti_pop=(anti/pop)*1000
gen interaction_pop=anti_pop*rou_pop

lab var rou_pop "Share of  exposed workers"
lab var anti_pop "Hate Incidents Per 1K Pop"
lab var interaction_pop "Exposed x Hate"

}

*******************************************************************************
* Analysis
*******************************************************************************
// Table 4: AfD Performance
{
global controls perc_hq perc_foreign perc_female  perc_manuf_trad_nocars perc_manuf_auto 

eststo clear

eststo: qui reg afp_prop rou_pop anti_pop , cluster(state_n) 

eststo: qui reg afp_prop rou_pop anti_pop perc_foreign $controls reg_south reg_east reg_north i.state_n, cluster(state_n) 

eststo: qui reg afp_prop rou_pop anti_pop interaction_pop     reg_south reg_east reg_north perc_foreign  $controls, cluster(state_n)
eststo: qui reg afp_prop rou_pop anti_pop interaction_pop     reg_south reg_east reg_north  i.state_n perc_foreign $controls, cluster(state_n)


esttab , replace label se title(AfD Performance \label {TableAfd})   compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rou_pop anti_pop interaction* )     indicate( "Other controls = perc_foreign" "FE State = *state_n" ) scalars( "N Observations" "r2 R$^2$" "aic AIC" )

esttab using "Table\AfD_high_pop_r2.tex", replace label se title(AfD Performance \label {TableAfd})   compress nogap star(* 0.1 ** 0.05 *** 0.01) b(%6.3f) keep(rou_pop anti_pop interaction* )     indicate( "Other controls = perc_foreign" "FE State = *state_n" ) scalars( "N Observations" "r2 R$^2$" "aic AIC" ) nomtitle collabels(none) 

	
}
*******************************************************************************
* Descriptives
*******************************************************************************
{

eststo clear

// table A16: Summary statistics of variables used in this study about AfD regional performance
{
estpost sum afp_prop rou_pop anti_pop  anti    reg_south reg_east reg_north  $controls, d

esttab , /// 
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(2)) max(label(Max) fmt(2)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs
eststo clear

esttab using "Table\AfD_descriptive.tex", /// 
	cells("mean(label(Mean) fmt(2)) p50(label(Median) fmt(2)) sd(label(S.D.) fmt(2)) min(label(Min.) fmt(2)) max(label(Max) fmt(2)) count(label(Obs.) fmt(0))") ///
	nonumber label replace noobs
eststo clear
}
}
